package cn.example.web.db.bean;

import lombok.experimental.Accessors;
import lombok.Data;
import lombok.AccessLevel;
import lombok.Setter;
import java.util.Date;
import org.artifact.core.db.BaseBean;
import org.artifact.core.lang.IServer;
import java.util.Map;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.map.MapUtil;
import java.util.HashMap;
@Data
@Accessors(chain = true)
@SuppressWarnings({"rawtypes","unchecked"})
/** 用户 */
public abstract class User implements BaseBean<User> {
	/** 主键 */
	private long id;
	/** 账号 */
	private String account;
	/** 密码 */
	private String password;
	/** 权限 */
	private String permission;
	/** 渠道ID */
	private long channelId;
	/** 渠道包ID */
	private long channelPackId;
	/** 设备Token */
	private String deviceToken;
	/** 设备名称 */
	private String deviceName;
	/** 设备ID */
	private String deviceId;
	/** 系统名称 */
	private String osName;
	/** 系统版本 */
	private String osVersion;
	/** 手机号 */
	private String phoneNumber;
	/** 邮箱 */
	private String email;
	/** 语言 */
	private String language;
	/** 客户端版本 */
	private String clientVersion;
	/** 创建时间 */
	@Setter(value = AccessLevel.PROTECTED)
	private java.util.Date createDate;
	/** 修改时间 */
	@Setter(value = AccessLevel.PROTECTED)
	private java.util.Date modifiedDate;
	
	


	


	@Override
	public User deepCopy() {
		User user = null;
		try {
			user = (User) super.clone();
			user.createDate = (java.util.Date)this.createDate.clone();
			user.modifiedDate = (java.util.Date)this.modifiedDate.clone();
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
		onCopy(user);
		return user;
	}

	public <T extends User> T build(User t){
	    if(t == null) return null;
	    BeanUtil.copyProperties(t, this);
        onRead();
        return (T)this;
	}

	public User toSave() {
        if(this.id == 0)
            this.id = (long)IServer.me().getContext().getIdFactory().getId();
		Date now = new Date();
		if (createDate==null) {
			createDate = now;
			modifiedDate = now;
		}else {
			modifiedDate = now;
		}
		onWrite();
		return deepCopy();
	}
	


    /**
     * 写数据
     */
    public Object write(){
        Object[] values = new Object[17];
        values[0] = this.id;
        values[1] = this.account;
        values[2] = this.password;
        values[3] = this.permission;
        values[4] = this.channelId;
        values[5] = this.channelPackId;
        values[6] = this.deviceToken;
        values[7] = this.deviceName;
        values[8] = this.deviceId;
        values[9] = this.osName;
        values[10] = this.osVersion;
        values[11] = this.phoneNumber;
        values[12] = this.email;
        values[13] = this.language;
        values[14] = this.clientVersion;
        values[15] = this.createDate;
        values[16] = this.modifiedDate;
        return values;
    }

    /**
     * 读数据
     */
    public User read(Object object){
        Object[] array = (Object[])object;
        this.id = (long)array[0];
        this.account = (String)array[1];
        this.password = (String)array[2];
        this.permission = (String)array[3];
        this.channelId = (long)array[4];
        this.channelPackId = (long)array[5];
        this.deviceToken = (String)array[6];
        this.deviceName = (String)array[7];
        this.deviceId = (String)array[8];
        this.osName = (String)array[9];
        this.osVersion = (String)array[10];
        this.phoneNumber = (String)array[11];
        this.email = (String)array[12];
        this.language = (String)array[13];
        this.clientVersion = (String)array[14];
        this.createDate = (java.util.Date)array[15];
        this.modifiedDate = (java.util.Date)array[16];
        return this;
    }
    /**
     * 克隆之后
     */
    protected void onCopy(User user){

    }

    /**
     * 写数据之前
     */
    protected void onWrite(){
    }

    /**
     * 读数据之后
     */
    protected void onRead(){
    }

	@Override
	public Object pk() {
		return this.id;
	}
}